package com.gs.process.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gs.process.admin.context.ProcessContext;
import com.gs.process.admin.service.ApprovalDetailsService;
import com.gs.process.admin.service.ProcessNodeService;
import com.gs.process.admin.utils.PageUtil;
import com.gs.process.model.ApprovalDetails;
import com.gs.process.model.ProcessNode;
import com.shallowUniverse.logs.annotation.AutoLog;
import com.shallowUniverse.model.Params;
import com.shallowUniverse.web.BaseController;
import com.shallowUniverse.web.annotation.AutoReturn;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Shuai Guo
 * @description:
 * @date: 2021/9/28
 * @version: 1.0
 */
@Controller
@RequestMapping("/approval/details")
public class ApprovalDetailsController extends BaseController {

    @Resource
    private ApprovalDetailsService approvalDetailsService;
    @Resource
    private ProcessNodeService processNodeService;
    @Resource
    private ProcessContext processContext;

    @AutoLog
    @AutoReturn
    @PostMapping("/save")
    public void save(@RequestBody ApprovalDetails approvalDetails, HttpServletRequest request) {
        boolean result = approvalDetailsService.save(approvalDetails, request);
        this.submit(result, approvalDetails.getSid());
    }

    @AutoLog
    @AutoReturn
    @PostMapping("/find")
    public void find(@RequestBody Params<ApprovalDetails> params) {
        //定义返回数据
        List<ApprovalDetails> dataList = new ArrayList<>();
        //判断是否分页
        if (params.isPaging()) {
            IPage<ApprovalDetails> page = approvalDetailsService.page(PageUtil.create(params));
            this.setTotal((int) page.getTotal());
        } else {
            dataList = approvalDetailsService.list();
            this.setTotal(dataList.size());
        }
        //设置转换信息
        dataList.forEach(approvalDetails -> {
            //设置审批人、审批角色、节点名
            ProcessNode processNode = processNodeService.getById(approvalDetails.getProcessNodeId());
            approvalDetails.setProcessNodeName(processNode.getName());
            approvalDetails.setResolvePerson(processContext.getUser(approvalDetails.getUserId()));
            approvalDetails.setResolveRole(processContext.getRole(processNode.getRoleId()));
        });
        this.submit(true, dataList);
    }

    @AutoLog
    @AutoReturn
    @PostMapping("/findById/{sid}")
    public void find(@PathVariable Long sid) {
        ApprovalDetails approvalDetails = approvalDetailsService.getById(sid);
        //设置审批人、审批角色、节点名
        ProcessNode processNode = processNodeService.getById(approvalDetails.getProcessNodeId());
        approvalDetails.setProcessNodeName(processNode.getName());
        approvalDetails.setResolvePerson(processContext.getUser(approvalDetails.getUserId()));
        approvalDetails.setResolveRole(processContext.getRole(processNode.getRoleId()));
        //返回
        this.submit(true, approvalDetails);
    }

    @AutoLog
    @AutoReturn
    @DeleteMapping("/delete")
    public void delete(@RequestBody Params<List<Long>> params) {
        boolean result = approvalDetailsService.remove(params.getBody());
        this.submit(result, null);
    }
}
